package com.doraro.platform.common.utils;

import com.doraro.platform.config.shiro.OauthRealm;
import com.doraro.platform.module.user.model.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.mgt.RealmSecurityManager;

public class ShiroUserUtil {

    public static SysUser getCurrentUser() {
        return (SysUser) SecurityUtils.getSubject().getPrincipal();
    }

    public static String getCurrentId() {
        return getCurrentUser().getId();
    }

    public static boolean is(String name) {
        return SecurityUtils.getSubject().hasRole(name);
    }

    public static void checkPermission(String permStr) throws AuthorizationException {
        SecurityUtils.getSubject().checkPermission(permStr);
    }

    public static void cleanCache() {
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        OauthRealm authRealm = (OauthRealm) rsm.getRealms().iterator().next();
        authRealm.clearCachedAuth(SecurityUtils.getSubject().getPrincipals());
    }
}
